package com.fpt.mockproject.j21.absd.model;

import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NaturalId;

import com.fpt.mockproject.j21.absd.util.MD5_Hashing;

@Entity
@Table(name = "USERS")
@DynamicUpdate(value = true)
public class User {
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "UserID", nullable = false, updatable = false, unique = true)
	private Long userID;
	
	@NaturalId
	@Column(name = "Account", unique = true, nullable = false)
	private String account;
	
	@Column(name = "Roles", nullable = false)
	private String role;
	
	@Column(name = "Password", nullable = false)
	private String password;
	
	@Column(name = "Email", unique = true, nullable = false)
	private String email;
	
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "addByUser")
	private Set<SupportingMaterial> supportingMaterials = new HashSet<>(0);
	
	public Set<SupportingMaterial> getSupportingMaterials() {
		return supportingMaterials;
	}
	public void setSupportingMaterials(Set<SupportingMaterial> supportingMaterials) {
		this.supportingMaterials = supportingMaterials;
	}
	public Long getUserID() {
		return userID;
	}
	public void setUserID(Long userID) {
		this.userID = userID;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password, boolean encrypt) {
		if(encrypt)
		{
			try {
				this.password = new MD5_Hashing(password).encrypt();
			} catch (NoSuchAlgorithmException e) {
			}
		}
		else
			this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}
